
/**
 * 左边导航项目鼠标移动过去后，显示的图片改变
 */
jQuery.fn.extend({
	highlight: function(search, insensitive, klass){
		var regex = new RegExp('(<[^>]*>)|(\\b'+ search.replace(/([-.*+?^${}()|[\]\/\\])/g,"\\$1") +')', insensitive ? 'ig' : 'g');
		return this.html(this.html().replace(regex, function(a, b, c){
			return (a.charAt(0) == '<') ? a : '<strong class="'+ klass +'">' + c + '</strong>'; 
		}));
	}
});

$(function(){
	    //个人设置
	    $('#personer').click(function(){
	    	try{
	    		var div = modifyPersonerDiv();
	    		win = MyUtils._getToolbarWin("tabs","personerId",div,modifyPersonerSubmit,"个人信息设置");
	    		win.window({
	    			width : 500,
	    			height : 300,
	    			modal : true
	    		});
	    		$('#personerId table th').css("text-align","right");
	    		$('#personerId table td input[type=text]').css("width","200");
	    		$('#personerId table td input[type=password]').css("width","200");
	    	} catch(e) {
	    		showJsException(e);
	    	}
	    });
	    
	    //返回主页
	    $('#backHome').click(function(){
	    	location.replace("index.form");
	    });
	    
	    //安全退出
	    $('#loginOut').click(function(){
	    	$.messager.confirm(_msg_sys, "确认退出本次登录？", function(r) {
				if(r) {
					location.replace("adminback/adminExit.form");
				}
	         });
	    });
	    
	    //加载欢迎框
	    $('#tabs').tabs('add',{
			title:'管理员信息提醒',
			content:createFrame("adminWelcome.adm"),
			closable:true,
			icon:'icon-sys'
		});
	    tabClose();
	    tabCloseEven();
	    
	    //加载左边导航
	    loadLeftMenu();
});

var modifyPersonerSubmit = function() {
	try {
		var dt = modifyPersonerValidator();
		
		if(dt != null) {
			MyUtils.openMask(_msg_process,win);
			var url = 'impl_modify/backModify.adm';
			var data = {
				 dt:dt
				,st:'admin_modify_personer'
				,qy:"[]"
			};
			MyUtils.sendPost(url, data,function(msg){
				MyUtils.hiddenMask(win);
				var json = MyUtils.toJsonObject(msg);
				if(json.error == 0) {
					$.messager.alert(_msg_sys,json.msg, "info");
					if(win) win.window("close");
				} else {
					$.messager.alert(_msg_sys,json.msg, "warning");
				}
			});
		}
	} catch (e) {
		showJsException(e);
		MyUtils.hiddenMask(win);
	}
};

var modifyPersonerValidator = function(){
	var record = {
			 aid:$('#uname').val()
			,amail:$('#umail').val()
			,apass:$('#newpwd').val()
			,apass2:$('#newpwd2').val()
			,oldpass:$('#oldpwd').val()
		};
	//验证用户账号
	var error = check_uname(record.aid);
	var msg = null;
	if(error > 0) {
		if (error == 1) {
			msg = ("用户名不能为空");
		} else if (error == 2) {
			msg = ("用户名为4-20位字符");
		} else if (error == 3) {
			msg = ("用户名为4-20位字符");
		} else if (error == 4) {
			msg = ("用户名格式错误");
		}
		$.messager.alert(_msg_sys,msg, "warning");
		return null;
	}
	
	//验证邮箱
	error = check_email(record.amail);
	if(error > 0) {
		msg = null;
		if (error == 1) {
			msg = ("Email不能为空");
		} else if (error == 2) {
			msg = ("邮箱格式错误");
		} else if (error == 3) {
			msg = ("邮箱长度不能超过100位");
		}
		$.messager.alert(_msg_sys,msg, "Warning");
		return null;
	}
	
	//验证密码
	if(!MyUtils.isEmpty(record.oldpass) || !MyUtils.isEmpty(record.apass) || !MyUtils.isEmpty(record.apass2)) {
		//旧密码验证
		if(MyUtils.isEmpty(record.oldpass) || MyUtils.MD5(record.oldpass) != _login_p) {
			$.messager.alert(_msg_sys,"旧密码输入错误！", "warning");
			return null;
		}
		//新密码验证
		error = MyUtils.check_pwd_by_name(record.apass,record.aid);
		if(error.error > 0) {
			$.messager.alert(_msg_sys,error.msg, "warning");
			return null; 
		}
		
		//新密码重复
		if(MyUtils.isEmpty(record.apass2) || (record.apass != record.apass2)) {
			$.messager.alert(_msg_sys,"确认密码输入错误", "warning");
			return null;
		}
		record.apass = MyUtils.MD5(record.apass);
	} 
	return MyUtils.toJsonStr(record);
};

function check_email(email) {
	if (email == "") {
		return 1;
	}
	var email_reg = /^\w[\w\$\^\(\)\[\]\{\}\.\-\+,]{0,100}@\w+(\.\w{2,4}){1,2}$/;
	if (!email_reg.test(email)) {
		return 2;
	}
	if (email.length > 100) {
		return 3;
	}
	return 0;
}

var check_uname = function(un) {
	var exception = /^[0-9a-zA-Z\,\$\^\(\)\-\_\+\[\]\{\}\u4e00-\u9fa5]{3,20}$/;
	if (MyUtils.isEmpty(un)) {
		return 1;
	}
	if (un.length < 4) {
		return 2;
	}
	if (un.length > 20) {
		return 3;
	}
	if (!exception.test(un)) {
		return 4;
	}
	return 0; 
};

var modifyPersonerDiv = function() {
	var d = '<div id="personerId" style="font-size:12px;text-align:left;margin-top:10px;width:450px;">'
			+'<table cellspacing="10" cellpadding="10">'
			+'<tr>'
			+'<th><label>用户名：</label></th>'
			+'<td><input  type="text" id="uname" name="uname" autocomplete="off" value="'+_login_u+'" maxlength="60"/><font color="red">*</font></td>'
			+'</tr>'
			+'<tr>'
			+'<th><label>Email地址：</label></th>'
			+'<td><input  type="text" id="umail" name="umail" autocomplete="off" value="'+_login_e+'" maxlength="200"/><font color="red">*</font></td>'
			+'</tr>'
			+'<tr>'
			+'<th><label>旧密码：</label></th>'
			+'<td><input  type="password" id="oldpwd" name="oldpwd" autocomplete="off" value="" maxlength="50"/><font color="red">*</font></td>'
			+'</tr>'
			+'<tr>'
			+'<th></th>'
			+'<td><font color="#768383" >如果要修改密码,请先填写旧密码,如留空,密码保持不变</font></td>'
			+'</tr>'
			+'<tr>'
			+'<th><label>新密码：</label></th>'
			+'<td><input  type="password" id="newpwd" name="newpwd" autocomplete="off" value="" maxlength="50"/><font color="red">*</font></td>'
			+'</tr>'
			+'<tr>'
			+'<th><label>确认密码：</label></th>'
			+'<td><input  type="password" id="newpwd2" name="newpwd2" autocomplete="off" value="" maxlength="50"/><font color="red">*</font></td>'
			+'</tr>'
			+'</table>'
			+'</div>';
	return d;
};

/**
 * 加载左边导航菜单
 */
function loadLeftMenu(){
	var div = '';
	if(navgation && navgation.length > 0) {
		//菜单
		$.each(navgation, function(i, n) {
			div += '<li class="category"><span style="font-weight:bold;">' + n.text + '</span>';
			var subDiv ='<ul>';
			var childers = n.children;
			if($.trim(n.url).length > 0 ||$.trim(n.jsp).length > 0 ){
				subDiv += '<li class="sub"><a href="'+n.url+'" title="'+n.text+'" jsp="'+n.jsp+'" icon="'+n.icon+'" menuId="'+n.id+'" >'
					+ '<span class="searchable"><label class="'+n.icon+'" style="margin:5px 0 0 0;height: 20px;width: 18px;float: left;" >&nbsp;</label>'+n.text+'</span></a></li>';
			}
			//菜单项
			$.each(childers, function(j, o) {
				subDiv += '<li class="sub"><a href="'+o.url+'" title="'+o.text+'" jsp="'+o.jsp+'" icon="'+o.icon+'" menuId="'+o.id+'" >'
					   +'<span class="searchable"><label class="'+o.icon+'" style="margin:5px 0 0 0;height: 20px;width: 18px;float: left;" >&nbsp;</label>'+o.text+'</span></a></li>';
			});
			subDiv += '</ul>';//end MenuItem
			
			div +=subDiv + "</li>"; //end Menu
		});
	}
	
	$(div).appendTo($('#static-list'));
	frame.initialize();
}

/**
 * 关闭右边的tab窗口
 */
function tabClose()
{
	/*双击关闭TAB选项卡*/
	$(".tabs-inner").dblclick(function(){
		var subtitle = $(this).children(".tabs-closable").text();
		$('#tabs').tabs('close',subtitle);
	});
	
	/*为选项卡绑定右键*/
	$(".tabs-inner").bind('contextmenu',function(e){
		$('#mm').menu('show', {
			left: e.pageX,
			top: e.pageY
		});

		var subtitle =$(this).children(".tabs-closable").text();
		$('#mm').data("currtab",subtitle);
		$('#tabs').tabs('select',subtitle);
		return false;
	});
}
function refresh(){
	var currTab = $('#tabs').tabs('getSelected');
	//菜单项新增的tab不存在ID
	var url = $(currTab.panel('options').content).attr('src');
	$('#tabs').tabs('update',{
		tab:currTab,
		options:{
			content:createFrame(url)
		}
	});
}
/**
 * 绑定Tab项的右键菜单事件
 */
function tabCloseEven()
{
	//刷新
	$('#mm-tabupdate').click(function(){
		refresh();
	});
	
	//关闭当前
	$('#mm-tabclose').click(function(){
		var currtab_title = $('#mm').data("currtab");
		$('#tabs').tabs('close',currtab_title);
	});
	
	//全部关闭
	$('#mm-tabcloseall').click(function(){
		$('.tabs-inner span').each(function(i,n){
			var t = $(n).text();
			$('#tabs').tabs('close',t);
		});
	});
	//关闭除当前之外的TAB
	$('#mm-tabcloseother').click(function(){
		$('#mm-tabcloseright').click();
		$('#mm-tabcloseleft').click();
	});
	//关闭当前右侧的TAB
	$('#mm-tabcloseright').click(function(){
		var nextall = $('.tabs-selected').nextAll();
		if(nextall.length==0){
			return false;
		}
		nextall.each(function(i,n){
			var t=$('a:eq(0) span',$(n)).text();
			$('#tabs').tabs('close',t);
		});
		return false;
	});
	//关闭当前左侧的TAB
	$('#mm-tabcloseleft').click(function(){
		var prevall = $('.tabs-selected').prevAll();
		if(prevall.length==0){
			return false;
		}
		prevall.each(function(i,n){
			var t=$('a:eq(0) span',$(n)).text();
			$('#tabs').tabs('close',t);
		});
		return false;
	});
}

/**
 * 添加 右边 tab 页
 * @param subtitle : 标题
 * @param url	：连接地址
 * @param icon	
 */
function addTab(subtitle,url,icon){
	if(!$('#tabs').tabs('exists',subtitle)){
		$('#tabs').tabs('add',{
			title:subtitle,
			content:createFrame(url),
			closable:true,
			icon:icon
		});
	}else{
		$('#tabs').tabs('select',subtitle);
		$('#mm-tabupdate').click();
	}
	tabClose();
}

/**
 * 创建右边操作的frame
 * @param url
 * @returns {String}
 */
function createFrame(url) {   
	var s = '<iframe scrolling="auto" frameborder="0"  src="'+url+'" style="width:100%;height:100%;"></iframe>';
	return s;
}